<template>
  <div class="save-content">
    <div class="save-card">
      <a-card size="small" class="card" :bordered="false" :hoverable="true">
        <a-table
          size="small"
          class="card"
          :dataSource="saveList"
          :columns="columns"
          rowKey="storeId"
          bordered
          :pagination="false"
          :customRow="customRow"
          :row-selection="{ selectedRowKeys: selectedKeyList, onChange: onSelectChange }"
        >
          <template #bodyCell="{ record, column }">
            <template v-if="column.dataIndex === 'modelId'">
              {{ getDictLabel(record.modelId, modelList, { value: 'modelId', label: 'modelName' }) }}
            </template>
          </template>
        </a-table>
      </a-card>
    </div>
    <div class="info-card">
      <div class="info-form">
        <a-card size="small" class="card" :bordered="false" :hoverable="true">
          <div class="list">
            <div class="item" v-for="item in formOption" :key="item.prop">
              <div class="label">{{ item.label }}:</div>
              <div class="span" v-if="item.format">{{ item.format(formData) }}</div>
              <div class="span" v-else>{{ formData[item.prop] || '-' }}</div>
            </div>
          </div>
        </a-card>
      </div>
      <div class="info-attr">
        <a-card size="small" class="card" :bordered="false" :hoverable="true">
          <a-table size="small" :dataSource="tableData" :columns="attrColumn" rowKey="attrId" bordered :loading="tableLoading" :pagination="false">
            <template #bodyCell="{ record, column }">
              <template v-if="column.dataIndex === 'modelId'">
                {{ getDictLabel(record.modelId, modelList, { value: 'modelId', label: 'modelName' }) }}
              </template>
              <template v-if="column.dataIndex === 'ts'">
                {{ dateFormat(record.ts) }}
              </template>
              <template v-if="column.dataIndex === 'time'">
                {{ dateFormat(record.time) }}
              </template>
            </template>
          </a-table>
          <div class="smart-query-table-page">
            <a-pagination
              showSizeChanger
              showQuickJumper
              show-less-items
              :pageSizeOptions="PAGE_SIZE_OPTIONS"
              :defaultPageSize="queryForm.pageSize"
              v-model:current="queryForm.pageNum"
              v-model:pageSize="queryForm.pageSize"
              :total="total"
              @change="queryData"
              @showSizeChange="queryData"
              :show-total="(total) => `共${total}条`"
            />
          </div>
        </a-card>
      </div>
    </div>
  </div>
</template>
<script setup>
  import { ref, onMounted, onUnmounted, watch, computed } from 'vue';
  import { getDictLabel, PageMixin } from '/@/utils/mixin';
  import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
  import { iotApi } from '/@/api/plat/dataStoreRule';
  import { iotApi as iotModelApi } from '/@/api/plat/dataModel';
  import { iotApi as iotAttrApi } from '/@/api/plat/dataModelAttr';
  import { dateFormat } from '/@/utils/date';

  const { total, tableLoading, tableData, selectedRowKeyList, resetQueryForm, queryTableData, deleteFn, batchDeleteFn, queryFormState, watchTotal } =
    PageMixin();

  let defalutData = {
    name: undefined,
  };
  let queryForm = ref(Object.assign({}, queryFormState, defalutData));
  watchTotal(queryForm, queryData);

  const saveList = ref([]);
  const modelList = ref([]);
  const selecteStoreId = ref(undefined);
  const modelId = ref(undefined);
  const selectedKeyList = computed(() => [selecteStoreId.value]);
  const columns = ref([
    {
      title: '关联数据模型',
      dataIndex: 'modelId',
    },
    {
      title: '目标存储表名',
      dataIndex: 'tableName',
    },
  ]);
  const attrColumn = ref([]);
  const formData = ref({});
  const formOption = ref([
    {
      label: '关联数据模型',
      prop: 'modelId',
    },
    {
      label: '目标存储表名',
      prop: 'tableName',
    },
    {
      label: '数据存储周期（天）',
      prop: 'retentionDays',
    },
    {
      label: '是否启用',
      prop: 'isEnable',
      format: (row) => {
        if (row.isEnable === true) {
          return '启用';
        } else if (row.isEnable === false) {
          return '禁用';
        } else {
          return '-';
        }
      },
    },
    {
      label: '规则描述',
      prop: 'description',
    },
  ]);

  // 每一行绑定点击事件
  const customRow = (record, index) => {
    return {
      onClick: () => {
        onSelectChange(null, [record]);
      },
    };
  };

  // 列选择
  function onSelectChange(selectedRowKeys, selectedRows) {
    if (selectedRows.length > 0) {
      selecteStoreId.value = selectedRows[0].storeId;
      modelId.value = selectedRows[0].modelId;
      getAttrList();
      formData.value = selectedRows[0];
      //
      queryForm.value.tableName = selectedRows[0].tableName;
      queryData();
    }
  }

  function getSaveList() {
    iotApi.queryList().then(({ data }) => {
      saveList.value = data;
    });
  }
  function getModelList() {
    iotModelApi.queryList().then(({ data }) => {
      modelList.value = data;
    });
  }
  function getAttrList() {
    iotAttrApi.queryList({ modelId: modelId.value }).then(({ data }) => {
      attrColumn.value = data.map((m) => {
        return {
          title: m.attrName,
          dataIndex: m.attrCode,
        };
      });
    });
  }

  // 查询数据
  function queryData() {
    console.log('queryForm.value: ', queryForm.value);
    const params = {
      current: queryForm.value.pageNum,
      size: queryForm.value.pageSize,
      tableName: queryForm.value.tableName,
    };
    queryTableData(iotApi.queryPage1, params);
  }

  onMounted(getSaveList);
  onMounted(getModelList);
</script>

<style scoped lang="less">
  .save-content {
    height: calc(100% - 20px);
    overflow: hidden;
    display: flex;
    justify-content: space-between;
    .save-card {
      width: 30%;
      height: 100%;
    }
    .info-card {
      width: calc(70% - 20px);
      height: 100%;
      .info-form {
        height: 200px;
        .list {
          height: 150px;
          display: flex;
          flex-wrap: wrap;
          flex-direction: column;
          .item {
            width: 50%;
            height: 50px;
            display: flex;
            align-items: center;
            .label {
              width: 150px;
              text-align: right;
              font-weight: 700;
              color: #000;
              margin-right: 10px;
            }
            .span {
              color: #000;
            }
          }
        }
      }
      .info-attr {
        height: calc(100% - 200px - 20px);
        margin-top: 20px;
      }
    }
  }
  .card {
    height: 100%;
  }
  ::v-deep .ant-card-body {
    height: 100%;
  }
</style>
